
package com.nbgroup.aspect;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.nbgroup.annotation.HttpMethod;
import com.nbgroup.context.BaseContext;
import com.nbgroup.mapper.OperlogMapper;
import com.nbgroup.pojo.vo.OperLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.annotation.Annotation;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class OperLogAspect {
    //@Pointcut("execution(* com.nbgroup.controller.*.*(..))")
    @Pointcut("@annotation(com.nbgroup.annotation.HttpMethod)")
    private void pointCut() {
    }


    @Autowired
    private OperlogMapper operlogMapper;

    @After("pointCut() && @annotation(httpMethod)")
    public void operLog(JoinPoint joinPoint, HttpMethod httpMethod) {

        String value = httpMethod.value();

        OperLogVo operLogVo = new OperLogVo();

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ipAddress = request.getRemoteAddr(); // 获取IP地址
        StringBuffer requestURL = request.getRequestURL();//访问地址

        // 获取 @GetMapping 注解
//        GetMapping getMapping = method.getAnnotation(GetMapping.class);
//        if (getMapping != null) {
//            String[] paths = getMapping.value();
//            if (paths.length > 0) {
//                operLogVo.setRequestMethod(paths[0]);
//            }
//        }
        operLogVo.setRequestMethod(value);

        // 获取 @ApiOperation 注解
        ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
        if (apiOperation != null) {
            operLogVo.setTitle(apiOperation.value());
        }


        // 获取当前用户名
        Long id = BaseContext.getCurrentId();
        String name = operlogMapper.getName(id.intValue());

        operLogVo.setOperName(name);



        //operLogVo.setRequestMethod(annotation);
        operLogVo.setOperUrl(String.valueOf(requestURL));
        //operLogVo.setTitle(modName);
        operLogVo.setOperIp(ipAddress);
        operLogVo.setOperTime(LocalDateTime.now());

        log.info("记录操作日志");
        operlogMapper.insert(operLogVo);

    }
}

